/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package model.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import model.dao.services.ProjectCategoryDAOServices;
import model.enetities.ProjectCategory;

/**
 *
 * @author Ku81
 */
public class  ProjectCategoryDAO implements ProjectCategoryDAOServices{
    private static ProjectCategoryDAO projectCategoryDAO;

    public static ProjectCategoryDAO getInstance() {
        if (projectCategoryDAO == null) {
            projectCategoryDAO = new ProjectCategoryDAO();
        }
        return projectCategoryDAO;
    }

    @Override
    public List<ProjectCategory> getProjectCategory(boolean isActived) {
        List<ProjectCategory> listProjectCategory = new ArrayList<ProjectCategory>();
        try {
            Connection conn = ConnectionFactory.getConnection();
            String sql = "select * from tblProjectCategory where isActived = ?";
            PreparedStatement pstmt = conn.prepareStatement(sql);
            
            ResultSet rs = pstmt.executeQuery();
            while (rs.next()) {
                model.enetities.ProjectCategory r = new model.enetities.ProjectCategory();
                r.setIdProjectCategory(rs.getInt("idProjectCategory"));
                r.setName(rs.getString("name"));
                r.setDescriptions(rs.getString("descriptions"));
                
                listProjectCategory.add(r);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return listProjectCategory;
    }

    @Override
    public ProjectCategory getProjectCategoryByID(int idProjectCategory,boolean isActived) throws Exception {
   
            ProjectCategory pc = new ProjectCategory();
            Connection conn = ConnectionFactory.getConnection();
            String sql = "select * from tblProjectCategory where idProjectCategory = ? and isActived = 'true'";
            PreparedStatement pstmt = conn.prepareStatement(sql);
            pstmt.setInt(1, idProjectCategory);
            ResultSet rs = pstmt.executeQuery();
            while (rs.next()) {
                pc.setIdProjectCategory(rs.getInt("idProjectCategory"));
                pc.setName(rs.getString("name"));
                pc.setName(rs.getString("descriptions"));

            
        }
            return pc;
    }
    @Override
    public boolean createProjectCategory(ProjectCategory projectCategory) {
        boolean isCheck = false;
        try {
 
            Connection conn = ConnectionFactory.getConnection();
            String sql = "insert into tblProjectCategory values (?,?,?)";
            PreparedStatement pstmt = conn.prepareStatement(sql);
            pstmt.setString(1, projectCategory.getName());
            pstmt.setString(2, projectCategory.getDescriptions());
            pstmt.setBoolean(3, projectCategory.isActived());
            
            pstmt.executeUpdate();
            isCheck = true;
        } catch (SQLException ex) {
            Logger.getLogger(NewsDAO.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(NewsDAO.class.getName()).log(Level.SEVERE, null, ex);
        }
        return isCheck; 
    }
    

    @Override
    public boolean upadteProjectCategory(ProjectCategory projectCategory) {
    
        boolean isCheck = false;
        try {
            //throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
            Connection con = ConnectionFactory.getConnection();
            String sql = "update tblProjectCategory set name = ?, descriptions = ?, isActived = ? where idProjectCategory = ? ";
            PreparedStatement pstmt = con.prepareStatement(sql);
            pstmt.setString(1, projectCategory.getName());
            pstmt.setString(2, projectCategory.getDescriptions());
            pstmt.setBoolean(3, projectCategory.isActived());
            pstmt.setInt(4, projectCategory.getIdProjectCategory());
            
            pstmt.executeUpdate();
            isCheck = true;
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(NewsDAO.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SQLException ex) {
            Logger.getLogger(NewsDAO.class.getName()).log(Level.SEVERE, null, ex);
        }
        return isCheck;
        
    }

    @Override
    public boolean deleteProjectCategory(int idProjectCategory) {
    
        boolean isCheck = false;
        try {
            Connection conn = ConnectionFactory.getConnection();
            String sql = "Update tblProjectCategory Set isActived = 'false' where idProjectCategory = ?";
            PreparedStatement pstmt = conn.prepareStatement(sql);
            pstmt.setInt(1, idProjectCategory );
            pstmt.executeUpdate();

            isCheck = true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return isCheck;
    }


  
}
